var express = require('express');
var router = express.Router();
var sql = require('../db/index')



/**
 * @swagger
 * /lanmu/add:
 *   post:
 *     description: 新增栏目接口
 *     tags: [栏目]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: name
 *         description: 栏目名称
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: 操作成功
 *       500:
 *         description: 服务器错误
 */

// 新增栏目接口
router.post('/add', async (req, res) => {
  const { name } = req.body;
  let mysql = `insert into lanmu (name) values ('${name}')`;
  try {
    await sql.query(mysql);
    res.send({
      status: 200,
      message: "新增栏目成功",
      data: null,
      timestamp: new Date().getTime()
    });
  } catch (error) {
    res.status(500).send({
      status: 500,
      message: "服务器错误，新增栏目失败",
      error: error.message,
      timestamp: new Date().getTime()
    });
  }
});

/**
 * @swagger
 * /lanmu/delete:
 *   post:
 *     description: 删除栏目接口
 *     tags: [栏目]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         description: 栏目id
 *         required: true
 *         type: number
 *     responses:
 *       200:
 *         description: 操作成功
 *       500:
 *         description: 服务器错误
 */

// 删除栏目接口
router.post('/delete', async (req, res) => {
  const { id } = req.body;
  let mysql = `delete from lanmu where id = ${id}`;
  try {
    await sql.query(mysql);
    res.send({
      status: 200,
      message: "删除栏目成功",
      data: null,
      timestamp: new Date().getTime()
    });
  } catch (error) {
    res.status(500).send({
      status: 500,
      message: "服务器错误，删除栏目失败",
      error: error.message,
      timestamp: new Date().getTime()
    });
  }
});

/**
 * @swagger
 * /lanmu/update:
 *   post:
 *     description: 修改栏目接口
 *     tags: [栏目]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         description: 栏目id
 *         required: true
 *         type: number
 *       - name: name
 *         description: 栏目名称
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: 操作成功
 *       500:
 *         description: 服务器错误
 */

// 修改栏目接口
router.post('/update', async (req, res) => {
  const { id, name } = req.body;
  let mysql = `update lanmu set name = '${name}' where id = ${id}`;
  try {
    await sql.query(mysql);
    res.send({
      status: 200,
      message: "修改栏目成功",
      data: null,
      timestamp: new Date().getTime()
    });
  } catch (error) {
    res.status(500).send({
      status: 500,
      message: "服务器错误，修改栏目失败",
      error: error.message,
      timestamp: new Date().getTime()
    });
  }
});

/**
 * @swagger
 * /lanmu/query:
 *   get:
 *     description: 查询所有栏目接口
 *     tags: [栏目]
 *     responses:
 *       200:
 *         description: 返回栏目列表
 */

// 查询所有栏目接口
router.get('/query', async (req, res) => {
  let mysql = `select * from lanmu`;
  try {
    let result = await sql.query(mysql);
    res.send({
      status: 200,
      message: "查询栏目成功",
      data: result,
      timestamp: new Date().getTime()
    });
  } catch (error) {
    res.status(500).send({
      status: 500,
      message: "服务器错误，查询栏目失败",
      error: error.message,
      timestamp: new Date().getTime()
    });
  }
});

module.exports = router;

